/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uoc.tdp.client.estudiants;

import edu.uoc.tdp.beans.conexio.Estudiant;
import edu.uoc.tdp.beans.proves.Prova;
import edu.uoc.tdp.common.RemotEstudiantsInterface;
import edu.uoc.tdp.common.TDSLanguageUtils;
import java.io.IOException;
import java.net.URL;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Jordi
 */
public class PantallaConsultaProves extends JFrame{
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private RemotEstudiantsInterface remot;
    private Estudiant estudiant;

    
    public PantallaConsultaProves() throws NotBoundException{
        

        try {
            Properties prop = new Properties();
            URL url = ClassLoader.getSystemResource("edu/uoc/tdp/properties/configuration.properties");
            prop.load(url.openStream());
            remot = (RemotEstudiantsInterface)Naming.lookup(prop.getProperty("pathrmi_estudiants"));
        } catch (IOException ex) {
            Logger.getLogger(PantallaConsultaProves.class.getName()).log(Level.SEVERE, null, ex);
        }
        
//TODO Falta activar serveis RMI en el servidor            
//            serveiRMIProves     = (RemotProvesInterface)Naming.lookup(prop.getProperty("pathrmi_proves"));
//            serveiRMIInformes   = (RemotInformesInterface)Naming.lookup(prop.getProperty("pathrmi_informes"));
       
        
        initComponents();
        carregainicial(); 
    }
    
   public PantallaConsultaProves(Estudiant pEstudiant,RemotEstudiantsInterface pRemot){
        estudiant = pEstudiant;
        remot = pRemot;
        
        setTitle(TDSLanguageUtils.getMessage("titol.Client")); 

        initComponents();
        
        jButton1.setText(TDSLanguageUtils.getMessage("boto.Sortir"));
        
        jButton2.setEnabled(true);
        
        setLocation(100, 100);
        
        carregainicial(); 
    }    
    
    private void initComponents() {

         jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("HOLA")); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

      /*  jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));*/
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(74, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        jButton1.setText("INSCRIURE"); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setText("SORTIR"); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(97, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(380, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(36, 36, 36)
                .addComponent(jButton2)
                .addGap(123, 123, 123))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();

    }
    
    private void carregainicial(){
         try {
           
            List<Prova> entrada = new ArrayList<Prova>();
            Prova prova = new Prova();
            
            entrada = remot.ObtenirProves();
            
            DefaultTableModel modelo = new DefaultTableModel();
            
            TDSLanguageUtils.setDefaultLanguage("edu/uoc/tdp/i18n/messages");
            
            modelo.addColumn(TDSLanguageUtils.getMessage("estudiants.pantalla.inscipcio.prova"));
            modelo.addColumn(TDSLanguageUtils.getMessage("estudiants.pantalla.inscipcio.tipus"));
            modelo.addColumn(TDSLanguageUtils.getMessage("estudiants.pantalla.inscipcio.preguntes"));
            modelo.addColumn(TDSLanguageUtils.getMessage("estudiants.pantalla.inscipcio.placestotals"));
            modelo.addColumn(TDSLanguageUtils.getMessage("estudiants.pantalla.inscipcio.placeslliure"));
            modelo.addColumn(TDSLanguageUtils.getMessage("estudiants.pantalla.inscipcio.datalimit"));
            
            for (int i = 0;i<entrada.size();i++) { 
 
                prova = entrada.get(i);
                Object[] fila = new Object[6];
                fila[0] = prova.getDescripcio(); 
                if(prova.getRondaEliminatoria() == null){
                    fila[1] = "FINAL";
                    fila[2] = prova.getRondaFinal().getNombrePreguntes();
                } else {
                    fila[1] = "ELIMINATORIA";
                    fila[2] = prova.getRondaEliminatoria().getNombrePreguntes();
                }
                 
                fila[3] = prova.getNombrePlaces();  
                fila[4] = prova.getNombrePlaces() - prova.getNombreInscrits();  
                fila[5] = prova.getDataFiInscripcio();  
               
                
                modelo.addRow(fila); 
            } 
            this.jTable1.setModel(modelo); 
                                    
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
 
   public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaConsultaProves.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaConsultaProves.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaConsultaProves.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaConsultaProves.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    new PantallaConsultaProves().setVisible(true);
                } catch (NotBoundException ex) {
                    Logger.getLogger(PantallaConsultaProves.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
}
